package servlets.user;

import hibernate.constraint.Constraint;
import hibernate.constraint.ErrorCodes;
import hibernate.method.UserMethods;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		HttpSession session = request.getSession();
		response.setContentType("text/html");
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session s = sessionFactory.openSession();
		s.beginTransaction();
		UserMethods um = new UserMethods();
		int valid = um
				.sign_up_validation(sessionFactory, s, username, password);
		switch (valid) {
		case ErrorCodes.IS_NOT_USER:
			out.println("<script type=\"text/javascript\">alert(\""+Constraint.SIGN_UP_FAILURE+ " - " + ErrorCodes.IS_NOT_USER_DESCRIPTION +"\"); window.location.href = \"../Thesis/index.jsp\";</script>");
			break;
		case ErrorCodes.SIGN_UP_FAIL:
			out.println("<script type=\"text/javascript\">alert(\""+Constraint.SIGN_UP_FAILURE+ " - " + ErrorCodes.SIGN_UP_DESCRIPTION +"\"); window.location.href = \"../Thesis/index.jsp\";</script>");
			break;
		case ErrorCodes.USER_INACTIVE:
			out.println("<script type=\"text/javascript\">alert(\""+Constraint.SIGN_UP_FAILURE+ " - " + ErrorCodes.USER_INACTIVE_DESCRIPTION +"\"); window.location.href = \"../Thesis/index.jsp\";</script>");
			break;
		default:
			//out.println("<script type=\"text/javascript\">alert(\""+Constraint.SIGN_UP_SUCCESS+ " - " + Constraint.SIGN_UP_SUCCESS +"\"); window.location.href = \"../Thesis/index.jsp\";</script>");
			session.setAttribute("user_id", valid);
			session.setAttribute("type",  um.getUserType(sessionFactory, s, valid));
			if ("client".equals( um.getUserType(sessionFactory, s, valid))) {
				out.println("<script type=\"text/javascript\">alert(\""+ Constraint.SIGN_UP_SUCCESS + " jako klient" +"\"); window.location.href = \"../Thesis/social/index.jsp\";</script>");
			} else if ("worker".equals( um.getUserType(sessionFactory, s, valid))) {
				out.println("<script type=\"text/javascript\">alert(\""+ Constraint.SIGN_UP_SUCCESS + " jako pracownik" +"\"); window.location.href = \"../Thesis/management/index.jsp\";</script>");
			} else if ("admin".equals( um.getUserType(sessionFactory, s, valid))) {
				out.println("<script type=\"text/javascript\">alert(\""+Constraint.SIGN_UP_SUCCESS + " jako admin" +"\"); window.location.href = \"../Thesis/admin/index.jsp\";</script>");
			} else
				out.println("<script type=\"text/javascript\">alert(\"" + Constraint.SIGN_UP_SUCCESS+"\"); window.location.href = \"../Thesis/index.jsp\";</script>");
			break;
		}
	}

}
